<?php

namespace App\Http\Controllers\Chanjet;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class TplusInventoryTurnoverAnalysis extends BaseController
{
    public function getExternalData(){
        $common = new TplusCommonController();
        $res = $common->getExternalData("TplusInventoryTurnoverAnalysis",$this->mangerinfo['org_id']);
        return $res;
    }
    public function getlist(Request $request)
    {
        $memberinfo = $this->mangerinfo;
        $isgetall = $request->isgetall ?? 0;

        $start_time = $request->start_time;
        $end_time = $request->end_time;
        $analysis_method = $request->analysis_method;
        if(!$start_time || !$end_time) {
            return response()->json(['code'=>'001','msg'=>'分析日期不能为空']);
        }
        if(!$analysis_method){
            return response()->json(['code'=>'001','msg'=>'分析方式不能为空']);
        }
        //分析期间  分析方式AnalysisMethod 313按照数量   314 按照金额
        $start_time = date('Y.m',strtotime($start_time));
        $end_time = date('Y.m',strtotime($end_time));

        $dic[] = ['VoucherDate',$start_time,$end_time];
        $dic[] = ['cpid',$memberinfo['cpid']];

        if($analysis_method){
            //313按照数量   314 按照金额
            if($analysis_method == "按照数量") $analysis_method = 313;
            if($analysis_method == "按照金额") $analysis_method = 314;
            $dic[] = ['AnalysisMethod',$analysis_method];
        }

        $checkData = [
            'controllerName'=>'TplusInventoryTurnoverAnalysis',
            'org_id'=>$this->mangerinfo['org_id'],
            'condition'=>$dic
        ];
        $reportDataCheck = $this->checkReportCache($checkData);
        if($reportDataCheck['code'] != '000'){
            return response()->json(['code'=>'001','msg'=>'T+数据请求失败,请检查']);
        }

        if($reportDataCheck['data']){
            $request->cache_ids = $reportDataCheck['data'];
        }else{
            $request->cache_ids = [];
        }

        $list = \App\Models\Chanjet\TplusInventoryTurnoverAnalysis::getList($request, $memberinfo['cpid'], $isgetall);

        $totalField = $this->fieldTotal($list['list'],
            ['quantity','stock_amount']
            ,['warehouse_name','warehouse_no']);
        $list['total'] = $totalField;
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }
}
